﻿#if UNITY_WX
using WeChatWASM;
#endif

namespace Core.Setting
{
    public partial class ScreenSetting
    {
        /// <summary>
        /// 微信
        /// </summary>
        private class ScreenWeChat : AbstractScreenPlatform
        {
            public override void Awake(ScreenSetting owner)
            {
                base.Awake(owner);

#if UNITY_WX
                var info = WX.GetWindowInfo();

                var top = (float)info.safeArea.top;
                var bottom = (float)info.safeArea.bottom;
                var left = (float)info.safeArea.left;
                var right = (float)info.safeArea.right;
                var width = (float)info.safeArea.width;
                var height = (float)info.safeArea.height;

                var windowWidth = (float)info.windowWidth;
                var windowHeight = (float)info.windowHeight;

                //float py = (float)info.safeArea.top / (float)info.windowHeight;

                //rect.anchorMin = new Vector2(left / windowWidth, 0.03f);
                //rect.anchorMax = new Vector2(right / windowWidth, 1 - top / windowHeight);
                var rect = new UnityEngine.Rect
                {
                    x = left,
                    y = windowHeight - bottom,
                    width = width,
                    height = height
                };

                Scrn.SafeArea = rect;
                Scrn.Size = new Core.WH(windowWidth, windowHeight);
                Scrn.Resolution = new Core.WH(windowWidth, windowHeight);

                log.err($"top={top},bottom={bottom},left={left},right={right},width={width},height={height}");
                log.err($"Screen.size={Scrn.Size}");
                log.err($"Screen.orientation ={UnityEngine.Screen.orientation}");

                //float py = (float)info.safeArea.top / (float)info.windowHeight;
                //// Rootrect初始时设置其Anchor，使其与父节点一样大，也就是屏幕的大小
                //// 调整屏幕移到刘海屏下面,
                //Rootrect.anchorMin = new Vector2((float)info.safeArea.left / (float)info.windowWidth, -(float)info.safeArea.top / (float)info.windowHeight );
                //// 重新计算缩放，让高度占满刘海屏以下的区域
                //cs.referenceResolution = new Vector2(cs.referenceResolution.x, cs.referenceResolution.y * (1.0f+py));
#endif
            }

            public override void Set(ESceneOrientation ori)
            {
                //TODO:
            }
        }
    }
}
